<script setup>
import { ref, useTemplateRef } from 'vue'

/** 暴露函数 */

defineExpose({
  openEnvelope,
})

/** 信封 */

const envelopeRef = useTemplateRef('envelopeRef')
const isShowTxt = ref(false)
const isOpenEnvelope = ref(false)

function openEnvelope() {
  if (isOpenEnvelope.value) {
    return
  } else {
    isOpenEnvelope.value = true
  }
  if (!envelopeRef.value) return
  envelopeRef.value.classList.add('open')

  setTimeout(() => {
    if (envelopeRef.value.parentElement) {
      envelopeRef.value.parentElement.classList.add('animate__animated', 'animate__backOutDown')
    }
  }, 3000)

  setTimeout(() => {
    isShowTxt.value = true
  }, 4000)
}
</script>
<template>
  <div class="box">
    <div class="out-border">
      <div class="out-box">
        <div class="txt" v-if:="isShowTxt">
          <p>亲爱的同学：</p>
          <p class="body-text">你好！</p>
          <p class="body-text">
            墨香飘逸，笔走龙蛇——书法社诚邀你一同感受汉字之美！无论你是书法高手还是零基础小白，这里都有志同道合的伙伴和丰富的活动等你加入。
          </p>
          <p class="body-text">执笔书青春，我们等你来！</p>
          <div class="align-right">
            <p>书法社​</p>
            <p>2025年5月6日</p>
          </div>
        </div>

        <div class="envelope-container" v-else>
          <div class="envelope" ref="envelopeRef">
            <div class="envelope-front"></div>
            <div class="envelope-back"></div>
            <div class="envelope-left"></div>
            <div class="envelope-right"></div>
            <div class="letter">
              <div class="letter-content"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<style scoped>
.box {
  position: absolute;
  top: 5%;
  left: 5%;
  width: 90%;
  height: 90%;

  display: flex;
}

.out-border {
  flex: 1;
  padding: 5px;
  box-sizing: border-box;
  background: linear-gradient(
    45deg,
    #f25953 12.5%,
    #fbfaf5 12.5%,
    #fbfaf5 25%,
    #5590d6 25%,
    #5590d6 37.5%,
    #fbfaf5 37.5%,
    #fbfaf5 50%,
    #f25953 50%,
    #f25953 62.5%,
    #fbfaf5 62.5%,
    #fbfaf5 75%,
    #5590d6 75%,
    #5590d6 87.5%,
    #fbfaf5 87.5%,
    #fbfaf5 100%
  );
  background-size: 70px 70px;
}

.out-box {
  width: 100%;
  height: 100%;
  background: #fbfaf5;

  display: flex;
  justify-content: center;
  align-items: center;
  perspective: 10rem;
}

.envelope-container {
  position: relative;
  width: 3rem;
  height: 2rem;
}

.envelope {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 1.5s ease-in-out;
}

.envelope-front {
  position: absolute;
  width: 0;
  height: 0;
  border-left: 1.5rem solid transparent;
  border-right: 1.5rem solid transparent;
  border-top: 1rem solid #d63031;
  transform-origin: top;
  transform: rotateX(0deg);
  transition: transform 1.5s ease-in-out;
  z-index: 3;
}

.envelope-back {
  position: absolute;
  width: 3rem;
  height: 2rem;
  background: #e17055;
  z-index: 1;
}

.envelope-left,
.envelope-right {
  position: absolute;
  width: 0;
  height: 0;
  bottom: 0;
  border-bottom: 1rem solid #c0392b;
  z-index: 2;
}

.envelope-left {
  left: 0;
  border-left: 1.5rem solid transparent;
}

.envelope-right {
  right: 0;
  border-right: 1.5rem solid transparent;
}

.letter {
  position: absolute;
  width: 2.8rem;
  height: 1.8rem;
  background: white;
  left: 0.1rem;
  top: 0.1rem;
  z-index: 0;
  padding: 0.1rem;
  box-sizing: border-box;
  transform: translateY(0);
  opacity: 0;
  transition:
    opacity 1s ease-in-out 1.5s,
    transform 1s ease-in-out 1.5s;
  box-shadow: 0 0 0.1rem rgba(0, 0, 0, 0.1);
}

.letter-content {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
  text-align: center;
}

.open .envelope-front {
  transform: rotateX(180deg);
}

.open .letter {
  opacity: 1;
  transform: translateY(-1rem);
}

/* 书信文字 */
.txt {
  flex: 1;
  font-size: 0.5rem;
  padding: 0.5rem;
}

.txt p {
  margin-bottom: 0.2rem;
}

.txt .body-text {
  text-indent: 2em;
  line-height: 1.5;
}

.txt .align-right {
  text-align: right;
}
</style>
